<?php
/**
 * Notes:
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/9/30
 * Time: 12:52
 */

namespace app\model;


use think\Model;

class FriendGroup extends Model
{


    public function addGroup($data){
//        $res = $this
//            ->validate(['friend_group_name' => 'require'],['friend_group_name.require' => '分组名不能为空'])
//            ->save($data);
//        return $res;
        return self::create($data);
    }

    public function addDefault($user_id,$time){
        $res = self::create([
            "friend_group_name" => "我的好友",
            "friend_group_default" => 1,
            "user_id" => $user_id,
            "create_time" => $time,
            "num" => 0,
            "online_num" => 0
        ]);
        return $res;
    }
    public function setNumInc($group_id){
        return self::get($group_id)->setInc('num',1);
    }
    public function setNumDec($group_id){
        return self::get($group_id)->setDec('num',1);
    }
    public function getFriendGroup($user_id){
        return self::where(['user_id' => $user_id])
            ->select();
    }
    public function del($id){
        return self::get($id)->delete();
    }
    public function edit($id,$data){
        $res = $this->allowField(true)->save($data,['friend_group_id'=>$id]);
        return $res;
    }
    public static function getDefault($user_id){
        return self::where([
            "friend_group_default" => 1,
            "user_id" => $user_id
        ])->select();
    }
    public static function check($user_id,$group_id){
        $res = self::where("user_id",'=',$user_id)->where('friend_group_id','=',$group_id)->count();
        return $res;
    }
    public function checkId($group_id){
        $res = self::where(['friend_group_id' => $group_id])->count();
        return $res;
    }
    public function checkDefault($group_id){
        $res = self::where(['friend_group_id' => $group_id])
            ->where(['friend_group_default' => 1])
            ->count();
        return $res;
    }

    public function getUserList($user_id){
        return self::where('user_id','=',$user_id)->select();
    }
}